#include <stdio.h>
#include <stdlib.h> 
#include <iostream>
#include <string.h>
#include <map>
using namespace std;

int test1();
int test2();
int test3();
int test4();
int main(int argc, char* argv[]) {
  test1();
  test2();
  test3();
  test4();
  return 0;
}

int test1() {
  map<int, int> xxxx;
  xxxx[1] = 100;
  xxxx[1] = 101;
  xxxx[8] = 109;
  printf("===========1===========\n");
  printf("%d\n",xxxx[1]);
  printf("%d\n",xxxx[8]);
  printf("%d\n",xxxx[2]);

  xxxx.erase(8);
  xxxx.erase(8);
  printf("===========1===========\n");
  printf("%d\n",xxxx[1]);
  printf("%d\n",xxxx[8]);
  printf("%d\n",xxxx[2]);
  printf("%d\n",xxxx[100]);

  int a = 1000;
  int * p = new int; p = &a;
  memcpy(&xxxx[1000],p,sizeof(int));
  printf("%d\n",xxxx[1000]);
  return 0;
}
int test2() { 
  cout << "==========2==========" << endl;
  map<string, string>  xxx;
  char szKey[10] = {0};
  char szBuf[20] = {0};
  xxx.insert(std::pair<string, string>("hellow","sfsfdsafdsfd"));
  xxx.insert(std::pair<string, string>("11111","dsafdsd"));

  cout <<"Key["<< xxx.begin()->first
       << "] Value[" <<xxx.begin()->second <<"]"<< endl;
  /**cout end()->first  will recv signal 11
   */
  cout << "Value[" <<xxx.end()->second <<"]"<< endl;

  map<string, string>::iterator it;
  it = xxx.find("11111");
  if(xxx.end() == it)
  {
    cout << "Can't find key!!" << endl;
  }
  else
  {
    cout <<"Key["<< it->first<< "] Value[" <<it->second <<"]"<< endl;
  }
  return 0;
}
int test3() {
  map<int, string> xxx;
  char szBuf[20] = {0};
  for (int i = 0; i < 1000000; i++)
  {
    memset(szBuf, '\0', 20);
    snprintf(szBuf, 20, "%d_value", i+2);
    xxx.insert(std::pair<int, string>(i, szBuf));
  }

  map<int, string>::iterator it;

#if 0
  //  通过迭代器删除
  for(it = xxx.begin(); it != xxx.end();)
  {
    xxx.erase(it++);
  }
  /**
   * 不能这样用
   *for(it = xxx.begin(); it != xxx.end(); it++)
   *{
   *   xxx.erase(it);
   *}
   */
#endif
#if 0
  //  通过key删除
  for(int i = 0; i< 1000000; i++)
  {
    xxx.erase(i);
  }
#endif
#if 0
  //  按范围删除
  xxx.erase(xxx.begin(), xxx.end());
#endif
#if 0
  //  clear方法
  xxx.clear();
#endif
#if 0
  //  empty方法判断
  while(!xxx.empty())
  {
    xxx.erase(xxx.begin());
  }
#endif
#if 1
  //  empty方法判断
  while(xxx.size() > 10)
  {
    xxx.erase(xxx.begin());
  }
#endif

  for(it = xxx.begin(); it != xxx.end(); it++)
  {
    cout <<"Key["<< it->first<< "] Value[" <<it->second <<"]"<< endl;
  }
  return 0;
}
int test4() {
  printf("=======test4=======\n");
  map<int, int> xxx;
  xxx[1] = 100;
  printf("[%d]\n",xxx[2]);
  if(0== xxx[3]); {
    printf("XXXXXXXXX[%d]\n",xxx[3]);
  }
  return 0;
}
